﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DTO.Models
{
    public class MediaProvider
    {
        CITDBDataContext db = new CITDBDataContext();

        public void save()
        {
            if (db != null)
                db.SubmitChanges();
        }

        public void Update(MEDIA obj)
        {
            MEDIA source = db.MEDIAs.SingleOrDefault(c => c.ID == obj.ID);
            if (obj != null)
            {
                source.Title = obj.Title == null ? "" : obj.Title;
                source.MediaID = Common.RemoveUnicode(obj.Title);
                source.Intro = obj.Intro == null ? "" : obj.Intro;
                source.Content = obj.Content == null ? "" : obj.Content;
                source.DateCreate = obj.DateCreate == null ? DateTime.Now : obj.DateCreate;
                source.IsPublish = obj.IsPublish == null ? false : obj.IsPublish;
                source.IsTop = obj.IsTop == null ? false : obj.IsTop;
                source.IsDelete = obj.IsDelete == null ? false : obj.IsDelete;
                source.TypeMediaName = obj.TypeMediaName;
                source.UserCreate = obj.UserCreate;
                source.DateModify = obj.DateModify;
                source.UserModify = obj.UserModify;
                save();
            }
        }

        public void UpdateTop(MEDIA obj, bool isTop)
        {
            MEDIA source = db.MEDIAs.SingleOrDefault(c => c.ID == obj.ID);
            if (obj != null)
            {
                source.IsTop = isTop;
                save();
            }
        }

        public void Insert(MEDIA obj)
        {
            if (!String.IsNullOrEmpty(obj.Title))
                obj.MediaID = Common.RemoveUnicode(obj.Title);
            obj.DateCreate = DateTime.Now;
            obj.DateModify = obj.DateCreate;
            db.MEDIAs.InsertOnSubmit(obj);
            save();
        }

        public void Delete(MEDIA obj)
        {
            db.MEDIAs.DeleteOnSubmit(obj);
        }

        public void Delete(string listid)
        {
            List<MEDIA> lst = new List<MEDIA>();
            MEDIA obj = null;
            string[] arr = listid.Split('-');

            DataRepository dr = new DataRepository();
            for (int i = 0; i < arr.Length - 1; i++)
            {
                obj = db.MEDIAs.SingleOrDefault(c => c.ID == Convert.ToInt32(arr[i]) && c.IsDelete == false);
                if (obj != null)
                    lst.Add(obj);
            }

            db.MEDIAs.DeleteAllOnSubmit(lst);
            save();
        }

        public void DeleteMark(string listid)
        {
            MEDIA obj = null;
            string[] arr = listid.Split('-');

            DataRepository dr = new DataRepository();
            for (int i = 0; i < arr.Length - 1; i++)
            {
                obj = db.MEDIAs.SingleOrDefault(c => c.ID == Convert.ToInt32(arr[i]) && c.IsDelete == false);
                if (obj != null)
                    obj.IsDelete = true;
            }

            save();
        }

        public MEDIA getMedia(int pID)
        {
            try
            {
                return db.MEDIAs.SingleOrDefault(c => c.ID == pID && c.IsDelete == false);
            }
            catch
            {
                return null;
            }
        }

        public MEDIA GetMediaByMediaID(string mediaID)
        {
            try
            {
                return db.MEDIAs.SingleOrDefault(c => c.MediaID == mediaID && c.IsDelete == false);
            }
            catch
            {
                return null;
            }
        }

        public MEDIA getMediaBanner(int pID)
        {
            try
            {
                return db.MEDIAs.SingleOrDefault(c => c.ID == pID && c.TypeMediaName.Contains("banner"));
            }
            catch
            {
                return null;
            }
        }

        public MEDIA getMediaIntro(int pID)
        {
            try
            {
                return db.MEDIAs.SingleOrDefault(c => c.ID == pID && c.TypeMediaName.Contains("intro"));
            }
            catch
            {
                return null;
            }
        }

        public IQueryable<MEDIA> getMedia(string pTypeMediaName)
        {
            try
            {
                return db.MEDIAs.Where(c => c.TypeMediaName == pTypeMediaName && c.IsDelete == false).OrderByDescending(c => c.DateCreate);
            }
            catch
            {
                return null;
            }
        }

        public IQueryable<MEDIA> getMediaAll(string pTypeMediaName)
        {
            try
            {
                return db.MEDIAs.Where(c => c.TypeMediaName == pTypeMediaName).OrderByDescending(c => c.DateCreate);
            }
            catch
            {
                return null;
            }
        }

        public IQueryable<MEDIA> getMediaByContainsTypeMediaName(string pTypeMediaName)
        {
            try
            {
                return db.MEDIAs.Where(c => c.TypeMediaName.Contains(pTypeMediaName)).OrderByDescending(c => c.DateCreate);
            }
            catch
            {
                return null;
            }
        }

        public IQueryable<MEDIA> getMedia(string pTypeMediaName, string pTitle, DateTime pDateFrom, DateTime pDateTo)
        {
            try
            {
                if (String.IsNullOrEmpty(pTitle) == false)
                {
                    return db.MEDIAs.Where(c => c.TypeMediaName == pTypeMediaName
                                        && c.IsDelete == false
                                        && c.Title.Contains(pTitle)
                                        && c.DateCreate >= pDateFrom
                                        && c.DateCreate <= pDateTo.AddDays(1)).OrderByDescending(c => c.DateCreate);
                }
                return db.MEDIAs.Where(c => c.TypeMediaName == pTypeMediaName
                                        && c.IsDelete == false
                                        && c.DateCreate >= pDateFrom
                                        && c.DateCreate <= pDateTo.AddDays(1)).OrderByDescending(c => c.DateCreate);
            }
            catch
            {
                return null;
            }
        }

        public IQueryable<MEDIA> getMedia(string pTypeMediaName, string pTitle, DateTime pDateFrom, DateTime pDateTo, bool pIsTop)
        {
            try
            {
                return db.MEDIAs.Where(c => c.TypeMediaName == pTypeMediaName
                                    && c.IsDelete == false
                                    && c.Title.Contains(pTitle)
                                    && c.DateCreate >= pDateFrom
                                    && c.DateCreate <= pDateTo.AddDays(1)
                                    && c.IsTop == pIsTop).OrderByDescending(c => c.DateCreate);
            }
            catch
            {
                return null;
            }
        }

        public IQueryable<MEDIA> getMedia(string pTypeMediaName, string pTitle, DateTime pDateFrom, DateTime pDateTo, bool pIsPublish, bool pIsTop, string pUserCreate)
        {
            try
            {
                if (String.IsNullOrEmpty(pTitle) == false)
                {
                    return db.MEDIAs.Where(c => c.TypeMediaName == pTypeMediaName
                                        && c.IsDelete == false
                                        && c.Title.Contains(pTitle)
                                        && c.DateCreate >= pDateFrom
                                        && c.DateCreate <= pDateTo.AddDays(1)
                                        && c.IsPublish == pIsPublish
                                        && c.IsTop == pIsTop
                                        && c.UserCreate.StartsWith(pUserCreate)).OrderByDescending(c => c.DateCreate);
                }
                return db.MEDIAs.Where(c => c.TypeMediaName == pTypeMediaName
                                        && c.IsDelete == false
                                        && c.DateCreate >= pDateFrom
                                        && c.DateCreate <= pDateTo.AddDays(1)
                                        && c.IsPublish == pIsPublish
                                        && c.IsTop == pIsTop
                                        && c.UserCreate.StartsWith(pUserCreate)).OrderByDescending(c => c.DateCreate);
            }
            catch
            {
                return null;
            }
        }


        #region "web ngoai"

        public MEDIA getMediaOut(string pTypeMediaName, string pMediaID)
        {
            try
            {
                return db.MEDIAs.SingleOrDefault(c => c.TypeMediaName == pTypeMediaName && c.IsDelete == false && c.IsPublish == true && c.MediaID == pMediaID);
            }
            catch
            {
                return null;
            }
        }

        public IQueryable<MEDIA> GetAllMediaOutBySearchText(string searchText)
        {
            try
            {
                return db.MEDIAs.Where(c => (c.Title.Contains(searchText) 
                    || c.MediaID.Contains(searchText))
                    && (c.TypeMediaName.Contains("tuvan")
                    || c.TypeMediaName.Contains("thongtin")
                    || c.TypeMediaName.Contains("daotao")
                    || c.TypeMediaName.Contains("hoatdong")
                    || c.TypeMediaName.Contains("sukien"))
                    && c.IsDelete == false 
                    && c.IsPublish == true).Distinct().OrderByDescending(c => c.DateCreate);
            }
            catch
            {
                return null;
            }
        }

        public IQueryable<MEDIA> getMediaOut(string pTypeMediaName)
        {
            try
            {
                return db.MEDIAs.Where(c => c.TypeMediaName == pTypeMediaName && c.IsDelete == false && c.IsPublish == true).OrderByDescending(c => c.DateCreate);
            }
            catch
            {
                return null;
            }
        }

        public IQueryable<MEDIA> getMediaTopOut(string pTypeMediaName)
        {
            try
            {
                return db.MEDIAs.Where(c => c.TypeMediaName.Contains(pTypeMediaName) && c.IsDelete == false && c.IsPublish == true && c.IsTop == true).OrderByDescending(c => c.DateCreate);
            }
            catch
            {
                return null;
            }
        }



        #endregion

    }
}